<?php

/**
 * Retrieve a module bootstrap instance from the initializer plugin.
 *
 * Use this if you want to invoke a method defined by a module bootstrap from
 * a controller or view script.
 *
 * This is automatically registered as an action and view helper by
 * Mz_Module_Initializer. Don't register this by itself, it's won't be very
 * useful.
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @package   Mz
 * @category  Module
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Module.php 38 2009-02-18 17:12:39Z monzee $
 */
class Mz_Module_Helper_Module extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * Get module instance from module initializer
     *
     * @param string $module module name
     * @return mixed The module bootstrap instance, false if not found, the
     *               module initializer plugin if no parameters are passed
     */
    public function direct($module = null)
    {
        $module = $this->_normalizeModuleName($module);
        $plugin = $this->getFrontController()->getPlugin('Mz_Module_Initializer');

        if (null === $module) {
            return $plugin;
        }

        return $plugin->getModule($module);
    }

    /**
     * Double as a view helper, same function as above
     * 
     * @param string $module 
     * @return mixed The module bootstrap instance, false if not found, the
     *               module initializer plugin if no parameters are passed
     */
    public function module($module = null)
    {
        return $this->direct($module);
    }

    protected function _normalizeModuleName($module)
    {
        return strtolower($module);
    }
}
